package com.kgis.fm.common.manager {

    import com.kgis.fm.flex.model.constant.LayerTypeConstant;
    import com.kgis.fm.flex.model.term.LegendEntry;
    import com.kgis.map.Map;
    import com.kgis.map.handler.feature.draw.DrawHandler;
    import com.kgis.map.layer.FeatureLayer;
    import com.kgis.widgets.managers.ErrorManager;
    import com.kgis.fm.flex.model.manager.FmMetaData;
   
    
    import mx.collections.ArrayCollection;

    /**
     * 画设施辅助工具类
     * @author zhxiaomiao
     * */
    public class DrawFeatureManager {
        public function DrawFeatureManager() {
            //throw
        }

        /**
         *@@param entityTypeId
         * */
        public static function getSnapLayerIdsByEntityTypeId(layerId:Number, map:Map):ArrayCollection {
            var snapLayerIds:ArrayCollection = new ArrayCollection();
            var layers:Vector.<FeatureLayer> = map.featureLayers;
            var relations:ArrayCollection = MetaDataManager.getRelationshipLayerIds(layerId);
            for (var m:int = 0; m < layers.length; m++) {
                var gmlegendEntry:LegendEntry = MetaDataManager.getLegendEntryById(layers[m].id);
                for (var j:int = 0; j < relations.length; j++) {
                    if (gmlegendEntry.leno == relations.getItemAt(j) && gmlegendEntry.layerType != LayerTypeConstant.LAYERTYPE_TEXT) {
                        snapLayerIds.addItem(layers[m].id);
                    }
                }
            }
            return snapLayerIds;
        }

        public static function getLayerByEntityTypeId(layerId:Number, map:Map):FeatureLayer {
            var layer:FeatureLayer = null;
            var layers:Vector.<FeatureLayer> = map.featureLayers;
            for (var i:int = 0; i < layers.length; i++) {
                var legendEntry:LegendEntry = MetaDataManager.getLegendEntryById(layers[i].id);
                if (legendEntry.leno == layerId && legendEntry.layerType != LayerTypeConstant.LAYERTYPE_TEXT) {
                    layer = layers[i];
                    break;
                }
            }
            return layer;
        }

        /**
         *@param entityTypeId
         *      get the draw entity by this entityTypeId.
         *@param map
         *      the draw entity's map
         * */
        public static function constructDrawHandler(layerId:Number, map:Map):DrawHandler {
            var drawLegendEntry:LegendEntry = null;
            var layers:Vector.<FeatureLayer> = map.featureLayers;
            var layer:FeatureLayer;
            for (var i:int = 0; i < layers.length; i++) {
                var legendEntry:LegendEntry = MetaDataManager.getLegendEntryById(layers[i].id);
                if (null != legendEntry) {
                    if (legendEntry.leno == layerId && legendEntry.layerType != LayerTypeConstant.LAYERTYPE_TEXT) {
                        drawLegendEntry = legendEntry;
                        layer = layers[i];
                        break;
                    }
                }
            }
            var metaData:FmMetaData = MetaDataManager.getInstance().getMetaData(MetaDataManager.KEY_KGM_METAData) as FmMetaData;
            var snapLayerIds:ArrayCollection = new ArrayCollection();
            var relations:ArrayCollection = MetaDataManager.getRelationshipLayerIds(layerId);
            for (var m:int = 0; m < layers.length; m++) {
                var gmlegendEntry:LegendEntry = MetaDataManager.getLegendEntryById(layers[m].id);
                if (null != gmlegendEntry) {
                    for (var j:int = 0; j < relations.length; j++) {
                        if (gmlegendEntry.leno== relations.getItemAt(j) && gmlegendEntry.layerType != LayerTypeConstant.LAYERTYPE_TEXT) {
                            snapLayerIds.addItem(layers[m].id);
                        }
                    }
                }
            }
            var drawHandler:DrawHandler;
            if (drawLegendEntry) {
                drawHandler = new DrawHandler(map, drawLegendEntry.layerType);
                drawHandler.drawLayer = layer;
                drawHandler.snapLayerIds = snapLayerIds;
            }
            return drawHandler;
        }

        public static function constructContinuousInputDrawLineHandler(layerId:Number, map:Map):DrawHandler {
            var drawLegendEntry:LegendEntry = null;
            var layers:Vector.<FeatureLayer> = map.featureLayers;
            var layer:FeatureLayer;
            for (var i:int = 0; i < layers.length; i++) {
                var legendEntry:LegendEntry = MetaDataManager.getLegendEntryById(layers[i].id);
                if (null != legendEntry) {
                    if (legendEntry.leno == layerId && legendEntry.layerType != LayerTypeConstant.LAYERTYPE_TEXT) {
                        drawLegendEntry = legendEntry;
                        layer = layers[i];
                        break;
                    }
                }
            }
            var drawHandler:DrawHandler;
            if (drawLegendEntry) {
                drawHandler = new DrawHandler(map, drawLegendEntry.layerType);
                drawHandler.drawLayer = layer;
            }
            return drawHandler;
        }
    }
}